package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.service.AskAnswerNameService;
import com.api.shopapi.service.CommandCenterStaffRelService;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 问卷调查名称
 * */

@RestController
@RequestMapping("AskAnswerName")
public class AskAnswerNameController extends BaseController{


    @Autowired
    @Qualifier("AskAnswerNameServiceImplOne")
    private AskAnswerNameService askAnswerNameService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("CommandCenterStaffRelServiceImplOne")
    private CommandCenterStaffRelService commandCenterStaffRelService;


    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;



    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            String type = jsonObject.getString("type");

            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerName/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);
            Staff staff = staffService.getByStaffCode(staffCode);

            AskAnswerName askAnswerName =new AskAnswerName();
            askAnswerName.setAskAnswerNameCode(UUID.randomUUID().toString());
            askAnswerName.setName(name);
            askAnswerName.setStatus(1);
            askAnswerName.setCreateTime(new Date());
            askAnswerName.setGovernmentCode(staff.getGovernmentCode());
            askAnswerName.setCommandCenterCode(commandCenterStaffRel.getCommandCenterCode());
            askAnswerName.setType(type);
            askAnswerNameService.insert(askAnswerName);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerName/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovAdd", method = RequestMethod.POST)
    public MyResult GovAdd(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            String type = jsonObject.getString("type");

            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerName/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            //CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            Staff staff = staffService.getByStaffCode(staffCode);

            AskAnswerName askAnswerName =new AskAnswerName();
            askAnswerName.setAskAnswerNameCode(UUID.randomUUID().toString());
            askAnswerName.setName(name);
            askAnswerName.setStatus(1);
            askAnswerName.setCreateTime(new Date());
            askAnswerName.setGovernmentCode(staff.getGovernmentCode());
            //askAnswerName.setCommandCenterCode(commandCenterStaffRel.getCommandCenterCode());
            askAnswerName.setType(type);
            askAnswerNameService.insert(askAnswerName);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerName/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int status = jsonObject.getInteger("status");
            String askAnswerNameCode = jsonObject.getString("askAnswerNameCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerName/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            AskAnswerName askAnswerName =askAnswerNameService.getByAskAnswerNameCode(askAnswerNameCode);
            askAnswerName.setName(name);
            askAnswerName.setStatus(status);
            askAnswerNameService.update(askAnswerName);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerName/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //指挥中心
    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult getList(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerName/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            List<AskAnswerName> list = askAnswerNameService.listByCommandCenterCode(commandCenterStaffRel.getCommandCenterCode());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",list);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerName/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovList", method = RequestMethod.POST)
    public MyResult GovList(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerName/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            List<AskAnswerName> list = askAnswerNameService.listByGovCodeType(staff.getGovernmentCode(),"train");

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",list);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerName/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //实践站点
    @RequestMapping(value = "ListForSite", method = RequestMethod.POST)
    public MyResult ListForSite(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String commandCenterCode = jsonObject.getString("commandCenterCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AskAnswerName/ListForSite");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<AskAnswerName> list = askAnswerNameService.listByCommandCenterCode(commandCenterCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",list);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerName/ListForSite",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //指挥中心
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerName/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            PageInfo<AskAnswerName> pageInfo = askAnswerNameService.pageByCommandCenterCode(pageIndex,pageSize,
                    commandCenterStaffRel.getCommandCenterCode());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerName/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovPage",method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerName/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<AskAnswerName> pageInfo = askAnswerNameService.pageByGovCodeType(pageIndex,pageSize,
                    staff.getGovernmentCode(),"train");

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerName/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovPageApp",method = RequestMethod.POST)
    public MyResult GovPageApp(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AskAnswerName/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);

            PageInfo<AskAnswerName> pageInfo = askAnswerNameService.pageByGovCodeType(pageIndex,pageSize,
                    user.getGovernmentCode(),"train");

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerName/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //小程序
    @RequestMapping(value = "PageXCX",method = RequestMethod.POST)
    public MyResult PageXCX(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String commandCenterCode = jsonObject.getString("commandCenterCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AskAnswerName/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            PageInfo<AskAnswerName> pageInfo = askAnswerNameService.pageByCommandCenterCode(pageIndex,pageSize,
                    commandCenterCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerName/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


}
